package DeviceData 
{
	import flash.desktop.NativeApplication;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import starling.display.Sprite
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
	import flash.events.ErrorEvent;
	import starling.events.Event;
	import flash.events.IOErrorEvent;
    import flash.events.MediaEvent;
	import flash.filesystem.File;
    import flash.media.CameraUI;
	import flash.media.MediaPromise;
    import flash.media.MediaType;
	import Screens.NavigationEvent;
	
	/**
	 * @author Simon Claeye
	 */
	
	public class CameraData extends Sprite
	{
        private var cam:CameraUI;
		private var image:Loader;
		private var imageNativePath:String;
		
		public function CameraData() 
		{
			if(CameraUI.isSupported)
            {
                cam = new CameraUI();
				cam.addEventListener(MediaEvent.COMPLETE, onComplete);
				cam.addEventListener(Event.CANCEL, onCanceled);
				cam.addEventListener(ErrorEvent.ERROR, onError);
            }
			else
				dispatchEvent(new CameraEvent(CameraEvent.UNSUPPORTED, false, false));
		}
		
		public function launchCamera():void
		{
			cam.launch(MediaType.IMAGE);
		}
		
		public function hasCapturedImage():Boolean
		{
			return (image != null);
		}
		
		public function getImage():Loader
		{
			return image;
		}
		
		public function getNativePath():String
		{
			return imageNativePath;
		}
		
		/**
		 * Callback function invoked when an image has been taken by the system camera. The function
		 * then proceeds to load that image.
		 * @param	e
		 */
		private function onComplete(e:MediaEvent):void
		{
			cam.removeEventListener(MediaEvent.COMPLETE, onComplete);
			cam.removeEventListener(Event.CANCEL, onCanceled);
			cam.removeEventListener(ErrorEvent.ERROR, onError);

			var mediaPromise:MediaPromise = e.data;
			var imgLoader:Loader = new Loader();
			
			if(mediaPromise.isAsync )
            {
				imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
				imgLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
				imageNativePath = mediaPromise.file.nativePath;
				imgLoader.loadFilePromise(mediaPromise);				
			}
			else
			{
				imgLoader.loadFilePromise(mediaPromise);
				image = imgLoader;
				imageNativePath = mediaPromise.file.nativePath;
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		/**
		 * Callback function for when the imagehas been loaded. It notifies the controller
		 * that an image has been taken.
		 * @param	event
		 */
		private function onLoaded(event:Object):void
		{
			var imgLoaderInfo:LoaderInfo = event.target as LoaderInfo;
			imgLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
			imgLoaderInfo.loader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			
			image = imgLoaderInfo.loader;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function onCanceled(event:Object):void
		{
			dispatchEvent(new Event(Event.CANCEL));
		}
		
		private function onError(event:ErrorEvent):void
		{
			trace("camera error: " + event.errorID +  " "+ event.text);
		}
		
		private function onLoadError(event:IOErrorEvent):void
		{
			event.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			event.target.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
			trace("camera load error: "+event.text);
		}
	}
}